<template>
  <a-drawer
    :title="title"
    :width="drawerWidth"
    @close="handleCancel"
    :visible="visible"
    :confirmLoading="confirmLoading">
    <div :style="{width: '100%',border: '1px solid #e9e9e9',padding: '10px 16px',background: '#fff',}">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          :label="menuLabel"
          hasFeedback >
          <a-input placeholder="请输入资源类型" v-decorator="[ 'type', validatorRules.type]" :readOnly="disableSubmit"/>
        </a-form-item>


        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="资源名称22">
          <a-input placeholder="请输入资源名称" v-decorator="[ 'name',validatorRules.name]" :readOnly="disableSubmit"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="资源url的匹配模板">
          <a-input placeholder="请输入资源url的匹配模板" v-decorator="[ 'patternValue',validatorRules.patternValue]" :readOnly="disableSubmit"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="资源url地址">
          <a-input placeholder="请输入资源url地址" v-decorator="[ 'value',validatorRules.value]" :readOnly="disableSubmit"/>
        </a-form-item>

<!--        <a-form-item-->
<!--          v-show="show"-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="菜单图标">-->
<!--          <a-input placeholder="点击选择图标" v-model="model.icon" :readOnly="disableSubmit">-->
<!--            <a-icon slot="addonAfter" type="setting" @click="selectIcons" />-->
<!--          </a-input>-->
<!--        </a-form-item>-->

<!--        <a-form-item-->
<!--          v-show="show"-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="排序">-->
<!--          <a-input-number placeholder="请输入菜单排序" style="width: 200px" v-decorator="[ 'sortNo',validatorRules.sortNo]" :readOnly="disableSubmit"/>-->
<!--        </a-form-item>-->

<!--        <a-form-item-->
<!--          v-show="show"-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="隐藏路由">-->
<!--          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="menuHidden"/>-->
<!--        </a-form-item>-->

<!--        <a-form-item-->
<!--          v-show="show"-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="聚合路由">-->
<!--          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="alwaysShow"/>-->
<!--        </a-form-item>-->

        <!--update_begin author:wuxianquan date:20190908 for:增加组件，外链打开方式可选 -->
<!--        <a-form-item-->
<!--          v-show="show"-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="打开方式">-->
<!--          <a-switch checkedChildren="外部" unCheckedChildren="内部" v-model="internalOrExternal"/>-->
<!--        </a-form-item>-->
        <!--update_end author:wuxianquan date:20190908 for:增加组件，外链打开方式可选 -->


      </a-form>

      <!-- 选择图标 -->
<!--      <icons @choose="handleIconChoose" @close="handleIconCancel" :iconChooseVisible="iconChooseVisible"></icons>-->
    </a-spin>
      <a-row :style="{textAlign:'right'}">
        <a-button :style="{marginRight: '8px'}" @click="handleCancel">
          关闭
        </a-button>
        <a-button :disabled="disableSubmit" @click="handleOk" type="primary">确定</a-button>
      </a-row>
    </div>
  </a-drawer>
</template>

<script>
  import {addPermission,editPermission,editSysResource,addSysResource,queryTreeList, duplicateCheck} from '@/api/api'
  import Icons from './icon/Icons'
  import pick from 'lodash.pick'

  export default {
    name: "PermissionModal",
    components: {Icons},
    data () {
      return {
        drawerWidth:700,
        treeData:[],
        treeValue: '0-0-4',
        title:"操作",
        visible: false,
        disableSubmit:false,
        model: {},
        // localMenuType:0,
        // alwaysShow:false,//表单元素-聚合路由
        // menuHidden:false,//表单元素-隐藏路由
        /*update_begin author:wuxianquan date:20190908 for:定义变量，初始值代表内部打开*/
        // internalOrExternal:false,//菜单打开方式
        /*update_end author:wuxianquan date:20190908 for:定义变量，初始值代表内部打开*/
        show:true,//根据菜单类型，动态显示隐藏表单元素
        menuLabel:'资源类型',
        isRequrie:true,  // 是否需要验证
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),

        // iconChooseVisible: false,
      }
    },
    computed:{
      validatorRules:function() {
        return {
          name:{rules: [{ required: this.show, message: '请输入资源名称!' }]},
          type:{rules: [{ required: true, message: '请输入资源类型!' }]},
          patternValue:{rules: [{ required: this.show, message: '请输入资源url的匹配模板!' }]},
          value:{rules: [{ required: this.show, message: '请输入资源url地址!' }]},

        }
      }
    },
    created () {
      this.initDictConfig();
    },
    methods: {
      // loadTree(){
      //   var that = this;
      //   queryTreeList().then((res)=>{
      //     if(res.success){
      //       console.log('----queryTreeList---')
      //       console.log(res)
      //       that.treeData = [];
      //       let treeList = res.result.treeList
      //       for(let a=0;a<treeList.length;a++){
      //         let temp = treeList[a];
      //         temp.isLeaf = temp.leaf;
      //         that.treeData.push(temp);
      //       }
      //     }
      //   });
      // },
      add () {
        // 默认值
        this.edit({status:'1',route:true});
      },
      edit (record) {
        this.resetScreenSize(); // 调用此方法,根据屏幕宽度自适应调整抽屉的宽度
        this.form.resetFields();
        this.model = Object.assign({}, record);
        //--------------------------------------------------------------------------------------------------
        //根据菜单类型，动态展示页面字段
        console.log(record)
        // this.alwaysShow = !record.alwaysShow?false:true;
        // this.menuHidden = !record.hidden?false:true;

        /*update_begin author:wuxianquan date:20190908 for:编辑初始化数据*/
        // if(record.internalOrExternal!=null){
        //   this.internalOrExt
        //   ernal = record.internalOrExternal?true:false;
        // }else{
        //   this.internalOrExternal = false;
        // }
        /*update_end author:wuxianquan date:20190908 for:编辑初始化数据*/


        //console.log('record.menuType', record.menuType);
        // this.show = record.menuType==2?false:true;
        // this.menuLabel = record.menuType==2?'按钮/权限':'菜单名称';

        // if(this.model.parentId){
        //   this.localMenuType = 1;
        // }else{
        //   this.localMenuType = 0;
        // }
        //----------------------------------------------------------------------------------------------

        this.visible = true;
        // this.loadTree();
        let fieldsVal = pick(this.model,'type','name','value','perms','value','url','status','patternValue');
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldsVal)
        });
      },
      close () {
        this.$emit('close');
        this.disableSubmit = false;
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            // this.model.alwaysShow = this.alwaysShow;
            // this.model.hidden = this.menuHidden;
            /*update_begin author:wuxianquan date:20190908 for:获取值*/
            // this.model.internalOrExternal = this.internalOrExternal;
            /*update_end author:wuxianquan date:20190908 for:获取值*/

            let formData = Object.assign(this.model, values);
            that.confirmLoading = true;
            console.log(formData);
            let obj;
            if (!this.model.id) {
              obj = addSysResource(formData);
            } else {
              obj = editSysResource(formData);
              console.log(formData);
            }
            obj.then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            });
          }
        })
      },
      handleCancel () {
        this.close()
      },

      // selectIcons(){
      //   this.iconChooseVisible = true
      // },
      // handleIconCancel () {
      //   this.iconChooseVisible = false
      // },
      // handleIconChoose (value) {
      //   console.log(value)
      //   this.model.icon = value
      //   this.form.icon = value
      //   this.iconChooseVisible = false
      // },
      // 根据屏幕变化,设置抽屉尺寸
      resetScreenSize(){
        let screenWidth = document.body.clientWidth;
        if(screenWidth < 500){
          this.drawerWidth = screenWidth;
        }else{
          this.drawerWidth = 700;
        }
      },
      initDictConfig() {
      },
    }
  }
</script>

<style scoped>

</style>
